package com.android.kotlincoroutinemvvmdemo.view
import android.content.Context
import android.graphics.Canvas
import android.graphics.Paint
import android.graphics.Path
import android.util.AttributeSet
import com.android.kotlincoroutinemvvmdemo.R

/*
* TIMLogoView
* */
class TIMLogoView @JvmOverloads constructor(
    context: Context,
    attributeSet: AttributeSet? = null
) : BaseView(context, attributeSet) {

    private val mPaint0 by lazy {
        Paint().apply {
            isAntiAlias = true
            isDither = true
            strokeCap = Paint.Cap.SQUARE
            color = context.resources.getColor(R.color.color_7B1FA2)
        }
    }

    override fun onDraw(canvas: Canvas?) {
        super.onDraw(canvas)
        setBackgroundColor(context.resources.getColor(R.color.color_F3F3F3))
        canvas?.apply {
            val cx = mWidth.toFloat() / 2
            val cy = mHeight.toFloat() / 2
            save()
            translate(cx, cy + cy / 14)

            val mStrokeWidth = (cx / 4).coerceAtLeast(minStrokeWidth)
            mPaint0.strokeWidth = mStrokeWidth
            val baseRadius = cx * 6 / 7
            val baseAngel = 360.0 / 5
            for (i in 1..pieces) {
                val cos0 = Math.cos(Math.toRadians(-90.0 + baseAngel * (i - 1))).toFloat()
                val sin0 = Math.sin(Math.toRadians(-90.0 + baseAngel * (i - 1))).toFloat()
                drawLine(
                    0F,
                    0F,
                    baseRadius * cos0,
                    baseRadius * sin0,
                    mPaint0
                )
            }

            restore()
        }
    }

    companion object {
        const val pieces = 5
    }
}